/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ola_cloud_m2m.h"
#include <stddef.h>
#include "cJSON.h"
#include "ola_def.h"
#include "ola_log.h"
#include "ola_time.h"
#include "ola_timer.h"
#include "ola_error.h"
#include "ola_link.h"
#include "ola_coap_net.h"
#include "ola_cloud_def.h"
#include "ola_cloud_common.h"
#include "ola_cloud_heartbeat.h"
#include "ola_cloud_token_refresh.h"

static bool g_isRunning = true;

static int CloudM2mInit(void)
{
    int ret;

    do {
        ret = CloudCheckNetworkInit(CLOUD_M2M_NETWORK_CHK_TIMEVAL);
        if (ret != OLA_OK) {
            OlaLogWarning("check network init error");
            break;
        }
        ret = CloudHeartbeatInit();
        if (ret != OLA_OK) {
            OlaLogWarning("heartbeat init error");
            break;
        }
        ret = CloudTokenRefreshInit();
        if (ret != OLA_OK) {
            OlaLogWarning("token refresh init error");
            break;
        }
        ret = OlaCloudResourceInit();
        if (ret != OLA_OK) {
            OlaLogWarning("cloud resource init error");
            break;
        }
        ret = OlaInitTimer();
        if (ret != OLA_OK) {
            OlaLogError("ola timer init error:%d", ret);
        }
        ret = OlaStartSyncTime();
        if (ret != OLA_OK) {
            OlaLogError("token refresh process error:%d", ret);
        }
        ret = OlaControlTimeSyncTimer(OLA_START_TIMER);
        if (ret != OLA_OK) {
            OlaLogWarning("ola time synctimer error");
            break;
        }
        g_isRunning = true;
        OlaPrintf("cloud m2m process enter\r\n");
    } while (false);

    return ret;
}

static int CloudM2mDeinit(void)
{
    int ret = OLA_OK;
    ret = OlaTimeDeinit();
    if (ret != OLA_OK) {
        OlaLogInfo("ola del time error [%d]", ret);
        return ret;
    }

    OlaDeinitTimer();
    OlaLinkParam *linkParam = OlaGetLinkParam(OLA_LINK_MODE_CLOUD);
    if ((linkParam == NULL) || (linkParam->ctx == NULL)) {
        OlaLogInfo("ctx already clear");
    } else {
        OlaCoapContext *ctx = linkParam->ctx;
        ret = OlaCoapFreeContext(ctx);
        if (ret != OLA_OK) {
            OlaLogCrit("coap context release error [%d]", ret);
        }
    }
    return ret;
}

/*
 * 功能: 云端通信模块退出
 */
void OlaCloudM2mExit(void)
{
    g_isRunning = false;
    OlaPrintf("cloud m2m process exit\r\n");
}

/*
 * 功能: 云端通信处理
 * 返回: 0，成功
 *       非0，错误码
 */
int OlaCloudM2mProcess(void)
{
    int ret;

    do {
        ret = CloudM2mInit();
        if (ret != OLA_OK) {
            OlaLogError("init error");
            break;
        }
        while (g_isRunning) {
            ret = CloudCheckNetworkProcess();
            if (ret != OLA_OK) {
                OlaLogError("network process error");
                break;
            }
            ret = OlaCoapProcess(CLOUD_COAP_WAIT_TIMEVAL);
            if (ret != OLA_OK) {
                OlaLogError("coap process error");
                break;
            }
            ret = CloudHeartbeatProcess();
            if (ret != OLA_OK) {
                OlaLogError("heartbeat process error");
                break;
            }
            ret = CloudTokenRefreshProcess();
            if (ret != OLA_OK) {
                OlaLogError("token refresh process error");
                break;
            }
            ret = OlaTimerProcess();
            if (ret != OLA_OK) {
                OlaLogError("ola timer process error");
                break;
            }
        }
    } while (false);

    (void)CloudM2mDeinit();
    return ret;
}